package cn.bigcontainer.alt.sys.extension;

import org.springframework.stereotype.Component;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.extension.MFormSubmission;
import cn.bigcontainer.alt.sys.dto.UserDto;

@Component
public class UserCreateExtension implements MFormSubmission {

    @Override
    public MViewDto before(MViewDto fv, Boolean create) {
        BigMap<String, Object> request = fv.getApp().getObject().getRequest();
        // 获取用户名和密码
        String id = request.getStringBySuffix("_sys_user_id");
        String password = request.getStringBySuffix("_sys_user_password");
        if (WoKit.isEmpty(password)) {
            password = UserDto.PWD;
        }
        // 并将密码MD5
        request.bigPutByPrefix("_sys_user_password", WoKit.getMD5(id, password));
        // 返回null表示继续后续提交，否则会阻止后续提交
        return null;
    }

    @Override
    public void after(MViewDto fv, Boolean create) {
    }
}
